<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//首页数据
class Home extends CommonController {

	function __construct(){
		
		parent::__construct();
		
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_total(){
		$user_id = $this->user_id;
		
		$bz = $this->input->post('bz',null,'人民币');
		//$date_month = $this->input->post('date_month',null,date('Y-m'));//获取当前期间
		
		if($bz == '人民币'){
			$curinfo = array(
				'name'=>'人民币',
				'exchange_rate'=>1,
				'mark'=>'￥'
			);
		}else{
			$this->load->model('User/CurrencyModel','CurrencyModel');
			$curinfo = $this->CurrencyModel->bz_get_info($bz);
			if(!$curinfo){
				$curinfo = array(
					'name'=>'人民币',
					'exchange_rate'=>1,
					'mark'=>'￥'
				);
			}
		}
		
		
		$this->load->model('Chuna/cashiersModel','cashiersModel');
		$info = array();

		//获取当前账号所能查询公司的权限
		$company_ids = $this->querys;

		$info['cash'] = $info['bank'] = 0;
		if($this->check_priv('chuna_cash|chuna_cash_1|chuna_bank|chuna_bank_1')){
			$info['cash'] = $this->get_total_cl(array('type'=>0,'company_ids'=>$company_ids),$curinfo['exchange_rate']);
			$info['bank'] = $this->get_total_cl(array('type'=>1,'company_ids'=>$company_ids),$curinfo['exchange_rate']);
		}
		
		//获取业务收入科目
		$in_subject = $this->get_income_subject();

		/*$this->load->model('User/companyModel','companyModel');
		//计算律所代收的
		$res = $this->companyModel->getAll(array('type'=>1));
		$money = 0;
		foreach($res as $val){
			if(in_array($val['id'],explode(',',$company_ids))){
				$aa = $this->get_total_cl(array('company'=>$val['name'],'sx_name'=>$val['sx_name'],'in_subject'=>$in_subject),$curinfo['exchange_rate']);
				$money += $aa;
			}
		}
		$info['lvsuo'] = $money;
		
		//计算风资的
		$res = $this->companyModel->getAll(array('type'=>0));
		$money = 0;
		foreach($res as $val){
			if(in_array($val['id'],explode(',',$company_ids))){
				$aa = $this->get_total_cl(array('company'=>$val['name'],'sx_name'=>$val['sx_name']),$curinfo['exchange_rate']);
				
				$money += $aa;
			}
		}
		$info['fengzi'] = $money;*/
		$this->load->model('User/companyModel','companyModel');
		$res = $this->companyModel->getAll();
		$lvsuo = $fengzi = 0;
		foreach($res as $val){
			$aa = 0;
			if(in_array($val['id'],explode(',',$company_ids))){
				
				if($val['type'] == 1){//计算律所代收的
					$aa = $this->get_total_cl(array('company'=>$val['sx_name'],'in_subject'=>$in_subject),$curinfo['exchange_rate'],1);
					$lvsuo += $aa;
				}else{//计算风资的
					$aa = $this->get_total_cl(array('company'=>$val['sx_name'],'in_subject'=>$in_subject),$curinfo['exchange_rate'],1);
					$fengzi += $aa;
				}

			}
		}
		$info['lvsuo'] = $lvsuo;
		$info['fengzi'] = $fengzi;
		
		$info['yewu'] = $info['fengzi'] + $info['lvsuo'];
		
		
		$info['mark'] = $curinfo['mark'];
		
		$this->data_out($info);
	}
	
	/*第三参数$is_insub是判断是否计算业务收入发生额的*/
	function get_total_cl($arr,$exchange_rate="1",$is_insub=0){
		
		//获取当前期间
		$date_month = $this->input->post('date_month',null,date('Y-m'));
		
		$total_before = 0;
		if(!$is_insub){
			//获取上期总余额
			$this->load->model('Chuna/CashBalanceModel','CashBalanceModel');
			$total_before = 0;
			$prev = $this->CashBalanceModel->get_prev_balance($date_month);
			if($prev){
				$total_before = $prev['money'];
			}
		}

		$income_arr = $arr;
		$income_arr['mtype'] = 0;
		$income_arr['date_month'] = $date_month;
		$income = $this->cashiersModel->get_totals($income_arr,'income');
		
		//因为业务收入发生额都是收入没有支出，所以没必要再去单独算支出的了
		if($is_insub){
			return toFloat($income * $exchange_rate);
		}
		
		$expenses_arr = $arr;
		$expenses_arr['mtype'] = 1;
		$expenses_arr['date_month'] = $date_month;
		$expenses = $this->cashiersModel->get_totals($expenses_arr,'expenses');
		
		$ls_num = $total_before + $income - $expenses;
		$ls_num = toFloat($ls_num * $exchange_rate);
		return $ls_num;
	}
	
	//获取业务收入发生额，当前期间汇总现金与银行日记账中科目为营业收入类（8开头）的科目收入总计
	function get_income_subject(){
		$this->load->model('User/SubjectModel','Subject');
		$res = $this->Subject->getAll(array('type'=>1),'code,name',100);
		
		$subject_name = array();
		foreach($res as $k=>$v){
			$subject_name[] = "'".$v['code'] . $v['name']."'";
		}
		return implode(',', $subject_name);

	}
	
	function get_company_bumen(){
		
		$company = $this->input->post('company',null,array());
		$bumen = $this->input->post('bumen',null,'');

		$companys = explode(',',$company);
		
		if(count($companys)<=0){
			$this->err_out('请选择主体公司');
		}
		
		if(empty($bumen)){
			$bumen = $this->admin['bumen'];
		}
		if(!is_array($bumen)){
			$bumen = explode(',',$bumen);
		}
		$bumen = array_filter($bumen);
		$companys = array_filter($companys);
		
		$this->load->model('Chuna/cashiersModel','cashiersModel');
		
		$arr = array();
		foreach($companys as $val){
			foreach($bumen as $bm){
				$income_total = $expenses_total = '';
				$income_total = $this->cashiersModel->get_totals(array('mtype'=>0,'company'=>$val,'bumen'=>$bm),'income');
				$expenses_total = $this->cashiersModel->get_totals(array('mtype'=>1,'company'=>$val,'bumen'=>$bm),'expenses');
				$arr[] = array(
					'company'=>$val,
					'bumen'=>$bm,
					//'data'=>array()
					'income'=>$income_total,
					'expenses'=>$expenses_total,
				);
			}
		}
		
		$this->data_out($arr);
	}
	
}
